<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.condition.chr_name"
        placeholder="名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />

      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
        v-purview-check="{modules: 'site_basedata',resources: 'site_basedata',operates:'add'}"
      >
        添加
      </el-button>
      <el-button
        v-waves
        :loading="downloadLoading"
        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="handleDownload"
         v-purview-check="{modules: 'site_basedata',resources: 'site_basedata',operates:'eaport'}"
      >
        导出
      </el-button>
    </div>

    <el-row :gutter="20">
      <el-col :span="3"
        ><div class="grid-content bg-purple">
          <el-tree
            :data="tree_data"
            node-key="id"
            :default-expand-all='true'
            :default-checked-keys="defaultselectKey"
            :props="defaultProps"
            @node-click="handleNodeClick"
          >
          </el-tree></div
      ></el-col>
      <el-col :span="21">
        <keep-alive>
          <component :is="currentRole" :listQuery="listQuery" />
        </keep-alive>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { BaseDataTypeList, BaseDataList } from "@/api/basedata";
import waves from "@/directive/waves"; // waves directive
import commonTable from "./components/commonTable";
import specialTable from "./components/specialTable";
export default {
  components: { commonTable, specialTable },
  directives: { waves },
  data() {
    return {
      currentRole: "commonTable",
      defaultselectKey:[],
      listQuery: {
        page_size: 20,
        page_index: 1,
        p_type_id: "",
        p_type_code: "",
        condition: {
          chr_name: "",
        },
      },
      tree_data: [
        {
          id: -1,
          chr_name: "基础数据",
          children: [],
        },
      ],
      defaultProps: {
        children: "children",
        label: "chr_name",
      },

      downloadLoading: false,
    };
  },
  methods: {
    handleNodeClick(data) {
      if (data.id == -1) {
        this.currentRole = "commonTable";
        this.listQuery.p_type_id = -1;

        return;
      }
      const sp_type_code = ["area", "industry"];
      if (sp_type_code.includes(data.chr_code)) {
        this.currentRole = "specialTable";
        this.listQuery.page_size = 99999;
      } else {
        this.currentRole = "commonTable";
        this.listQuery.page_size = 20;
      }
      this.listQuery.p_type_id = data.id;
      this.listQuery.p_type_code = data.chr_code;
      this.total = 0;
      this.listQuery.page_index = 1;
    },
    handleFilter() {},
    handleCreate() {},
    handleDownload() {},
    handleEdit() {},
    handleDelete() {},
    GetBaseDataTypeList() {
      BaseDataTypeList({ chr_name: "", chr_code: "" }).then((res) => {
        console.log(res);
        this.tree_data[0].children.push(...res.data);
        if (res.data.length > 0) {
          this.handleNodeClick(res.data[0]);
          this.defaultselectKey=[];
          this.defaultselectKey.push(res.data[0].id);
        }
      });
    },
  },
  mounted() {
    this.GetBaseDataTypeList();
  },
};
</script>
<style scoped>
</style>
